home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / snmplib / snmp_client.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  9.3 KB  |  315 lines

  1. /*
  2.  * snmp_client.c - a toolkit of common functions for an SNMP client.
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1988, 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <sys/param.h>
  28. #include <stdio.h>
  29. #include <netinet/in.h>
  30. #include <sys/time.h>
  31. #include <errno.h>
  32.  
  33. #include "asn1.h"
  34. #include "snmp.h"
  35. #include "snmp_impl.h"
  36. #include "snmp_api.h"
  37. #include "snmp_client.h"
  38.  
  39. #ifndef BSD4_3
  40. #define BSD4_2
  41. #endif
  42.  
  43. #ifndef BSD4_3
  44.  
  45. typedef long    fd_mask;
  46. #define NFDBITS    (sizeof(fd_mask) * NBBY)    /* bits per mask */
  47.  
  48. #define    FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  49. #define    FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  50. #define    FD_ISSET(n, p)    ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  51. #define FD_ZERO(p)    bzero((char *)(p), sizeof(*(p)))
  52. #endif
  53.  
  54.  
  55. extern int errno;
  56. struct synch_state snmp_synch_state;
  57.  
  58. struct snmp_pdu *
  59. snmp_pdu_create(command)
  60.     int command;
  61. {
  62.     struct snmp_pdu *pdu;
  63.  
  64.     pdu = (struct snmp_pdu *)malloc(sizeof(struct snmp_pdu));
  65.     bzero((char *)pdu, sizeof(struct snmp_pdu));
  66.     pdu->command = command;
  67.     pdu->errstat = SNMP_DEFAULT_ERRSTAT;
  68.     pdu->errindex = SNMP_DEFAULT_ERRINDEX;
  69.     pdu->address.sin_addr.s_addr = SNMP_DEFAULT_ADDRESS;
  70.     pdu->enterprise = NULL;
  71.     pdu->enterprise_length = 0;
  72.     pdu->variables = NULL;
  73.     return pdu;
  74. }
  75.  
  76. /*
  77.  * Add a null variable with the requested name to the end of the list of
  78.  * variables for this pdu.
  79.  */
  80. snmp_add_null_var(pdu, name, name_length)
  81.     struct snmp_pdu *pdu;
  82.     oid *name;
  83.     int name_length;
  84. {
  85.     struct variable_list *vars;
  86.  
  87.     if (pdu->variables == NULL){
  88.     pdu->variables = vars = (struct variable_list *)malloc(sizeof(struct variable_list));
  89.     } else {
  90.     for(vars = pdu->variables; vars->next_variable; vars = vars->next_variable)
  91.         ;
  92.     vars->next_variable = (struct variable_list *)malloc(sizeof(struct variable_list));
  93.     vars = vars->next_variable;
  94.     }
  95.  
  96.     vars->next_variable = NULL;
  97.     vars->name = (oid *)malloc(name_length * sizeof(oid));
  98.     bcopy((char *)name, (char *)vars->name, name_length * sizeof(oid));
  99.     vars->name_length = name_length;
  100.     vars->type = ASN_NULL;
  101.     vars->val.string = NULL;
  102.     vars->val_len = 0;
  103. }
  104.  
  105. snmp_synch_input(op, session, reqid, pdu, magic)
  106.     int op;
  107.     struct snmp_session *session;
  108.     int reqid;
  109.     struct snmp_pdu *pdu;
  110.     void *magic;
  111. {
  112.     struct variable_list *var, *newvar;
  113.     struct synch_state *state = (struct synch_state *)magic;
  114.     struct snmp_pdu *newpdu;
  115.  
  116.     if (reqid != state->reqid)
  117.     return 0;
  118.     state->waiting = 0;
  119.     if (op == RECEIVED_MESSAGE && pdu->command == GET_RSP_MSG){
  120.     /* clone the pdu */
  121.     state->pdu = newpdu = (struct snmp_pdu *)malloc(sizeof(struct snmp_pdu));
  122.     bcopy((char *)pdu, (char *)newpdu, sizeof(struct snmp_pdu));
  123.     newpdu->variables = 0;
  124.     var = pdu->variables;
  125.     if (var != NULL){
  126.         newpdu->variables = newvar = (struct variable_list *)malloc(sizeof(struct variable_list));
  127.         bcopy((char *)var, (char *)newvar, sizeof(struct variable_list));
  128.         if (var->name != NULL){
  129.         newvar->name = (oid *)malloc(var->name_length * sizeof(oid));
  130.         bcopy((char *)var->name, (char *)newvar->name, var->name_length * sizeof(oid));
  131.         }
  132.         if (var->val.string != NULL){
  133.         newvar->val.string = (u_char *)malloc(var->val_len);
  134.         bcopy((char *)var->val.string, (char *)newvar->val.string, var->val_len);
  135.         }
  136.         newvar->next_variable = 0;
  137.         while(var->next_variable){
  138.         newvar->next_variable = (struct variable_list *)malloc(sizeof(struct variable_list));
  139.         var = var->next_variable;
  140.         newvar = newvar->next_variable;
  141.         bcopy((char *)var, (char *)newvar, sizeof(struct variable_list));
  142.         if (var->name != NULL){
  143.             newvar->name = (oid *)malloc(var->name_length * sizeof(oid));
  144.             bcopy((char *)var->name, (char *)newvar->name, var->name_length * sizeof(oid));
  145.         }
  146.         if (var->val.string != NULL){
  147.             newvar->val.string = (u_char *)malloc(var->val_len);
  148.             bcopy((char *)var->val.string, (char *)newvar->val.string, var->val_len);
  149.         }
  150.         newvar->next_variable = 0;
  151.         }
  152.     }
  153.     state->status = STAT_SUCCESS;
  154.     } else if (op == TIMED_OUT){
  155.     state->status = STAT_TIMEOUT;
  156.     }
  157.     return 1;
  158. }
  159.  
  160.  
  161. /*
  162.  * If there was an error in the input pdu, creates a clone of the pdu
  163.  * that includes all the variables except the one marked by the errindex.
  164.  * The command is set to the input command and the reqid, errstat, and
  165.  * errindex are set to default values.
  166.  * If the error status didn't indicate an error, the error index didn't
  167.  * indicate a variable, the pdu wasn't a get response message, or there
  168.  * would be no remaining variables, this function will return NULL.
  169.  * If everything was successful, a pointer to the fixed cloned pdu will
  170.  * be returned.
  171.  */
  172. struct snmp_pdu *
  173. snmp_fix_pdu(pdu, command)
  174.     struct snmp_pdu *pdu;
  175.     int command;
  176. {
  177.     struct variable_list *var, *newvar;
  178.     struct snmp_pdu *newpdu;
  179.     int index, copied = 0;
  180.  
  181.     if (pdu->command != GET_RSP_MSG || pdu->errstat == SNMP_ERR_NOERROR || pdu->errindex <= 0)
  182.     return NULL;
  183.     /* clone the pdu */
  184.     newpdu = (struct snmp_pdu *)malloc(sizeof(struct snmp_pdu));
  185.     bcopy((char *)pdu, (char *)newpdu, sizeof(struct snmp_pdu));
  186.     newpdu->variables = 0;
  187.     newpdu->command = command;
  188.     newpdu->reqid = SNMP_DEFAULT_REQID;
  189.     newpdu->errstat = SNMP_DEFAULT_ERRSTAT;
  190.     newpdu->errindex = SNMP_DEFAULT_ERRINDEX;
  191.     var = pdu->variables;
  192.     index = 1;
  193.     if (pdu->errindex == index){    /* skip first variable */
  194.     var = var->next_variable;
  195.     index++;
  196.     }
  197.     if (var != NULL){
  198.     newpdu->variables = newvar = (struct variable_list *)malloc(sizeof(struct variable_list));
  199.     bcopy((char *)var, (char *)newvar, sizeof(struct variable_list));
  200.     if (var->name != NULL){
  201.         newvar->name = (oid *)malloc(var->name_length * sizeof(oid));
  202.         bcopy((char *)var->name, (char *)newvar->name, var->name_length * sizeof(oid));
  203.     }
  204.     if (var->val.string != NULL){
  205.         newvar->val.string = (u_char *)malloc(var->val_len);
  206.         bcopy((char *)var->val.string, (char *)newvar->val.string, var->val_len);
  207.     }
  208.     newvar->next_variable = 0;
  209.     copied++;
  210.  
  211.     while(var->next_variable){
  212.         var = var->next_variable;
  213.         if (++index == pdu->errindex)
  214.         continue;
  215.         newvar->next_variable = (struct variable_list *)malloc(sizeof(struct variable_list));
  216.         newvar = newvar->next_variable;
  217.         bcopy((char *)var, (char *)newvar, sizeof(struct variable_list));
  218.         if (var->name != NULL){
  219.         newvar->name = (oid *)malloc(var->name_length * sizeof(oid));
  220.         bcopy((char *)var->name, (char *)newvar->name, var->name_length * sizeof(oid));
  221.         }
  222.         if (var->val.string != NULL){
  223.         newvar->val.string = (u_char *)malloc(var->val_len);
  224.         bcopy((char *)var->val.string, (char *)newvar->val.string, var->val_len);
  225.         }
  226.         newvar->next_variable = 0;
  227.         copied++;
  228.     }
  229.     }
  230.     if (index < pdu->errindex || copied == 0){
  231.     snmp_free_pdu(newpdu);
  232.     return NULL;
  233.     }
  234.     return newpdu;
  235. }
  236.  
  237.  
  238. int
  239. snmp_synch_response(ss, pdu, response)
  240.     struct snmp_session *ss;
  241.     struct snmp_pdu *pdu;
  242.     struct snmp_pdu **response;
  243. {
  244.     struct synch_state *state = &snmp_synch_state;
  245.     int numfds, count;
  246.     fd_set fdset;
  247.     struct timeval timeout, *tvp;
  248.     int block;
  249.  
  250.  
  251.     if ((state->reqid = snmp_send(ss, pdu)) == 0){
  252.     *response = NULL;
  253.     snmp_free_pdu(pdu);
  254.     return STAT_ERROR;
  255.     }
  256.     state->waiting = 1;
  257.  
  258.     while(state->waiting){
  259.     numfds = 0;
  260.     FD_ZERO(&fdset);
  261.     block = 1;
  262.     tvp = &timeout;
  263.     timerclear(tvp);
  264.     snmp_select_info(&numfds, &fdset, tvp, &block);
  265.     if (block == 1)
  266.         tvp = NULL;    /* block without timeout */
  267.     count = select(numfds, &fdset, 0, 0, tvp);
  268.     if (count > 0){
  269.         snmp_read(&fdset);
  270.     } else switch(count){
  271.         case 0:
  272.         snmp_timeout();
  273.         break;
  274.         case -1:
  275.         if (errno == EINTR){
  276.             continue;
  277.         } else {
  278.             perror("select");
  279.         }
  280.         /* FALLTHRU */
  281.         default:
  282.         return STAT_ERROR;
  283.     }
  284.     }
  285.     *response = state->pdu;
  286.     return state->status;
  287. }
  288.  
  289. snmp_synch_setup(session)
  290.     struct snmp_session *session;
  291. {
  292.     session->callback = snmp_synch_input;
  293.     session->callback_magic = (void *)&snmp_synch_state;
  294. }
  295.  
  296. char    *error_string[6] = {
  297.     "No Error",
  298.     "Response message would have been too large.",
  299.     "There is no such variable name in this MIB.",
  300.     "The value given has the wrong type or length",
  301.     "This variable is read only",
  302.     "A general failure occured"
  303. };
  304.  
  305. char *
  306. snmp_errstring(errstat)
  307.     int    errstat;
  308. {
  309.     if (errstat <= SNMP_ERR_GENERR && errstat >= SNMP_ERR_NOERROR){
  310.     return error_string[errstat];
  311.     } else {
  312.     return "Unknown Error";
  313.     }
  314. }
  315.